require 'extract/rules/xml_rule'
require 'singleton'
require 'enum'

module Extract
  
  class FlickrRules < Enum
    
    public
    
    class NickName < XMLRule
      def xpath
        "/html/body/div[@id='Main']/div/table[@id='SubNav']/tr/h1//span[@class='nickname']"
        #"//span[@class='nickname']"
      end
      
      protected
      
      def handle_val(val, dest_receiver)
        dest_receiver.full_name = val
      end
    end
    
    class FirstName < XMLRule
      def xpath
        "/html/body/div[@id='Main']/div/table[@id='SubNav']/tr/h1//span[@class='given-name']"
        #"//span[@class='given-name']"
      end
      
      protected
      
      def handle_val(val, dest_receiver)
        dest_receiver.first_name = val
      end
    end
    
    class LastName < XMLRule
      def xpath
        "/html/body/div[@id='Main']/div/table[@id='SubNav']/tr/h1//span[@class='family-name']"
        #"//span[@class='family-name']"
      end
      
      protected
      
      def handle_val(val, dest_receiver)
        dest_receiver.last_name = val
      end
    end
    
    class Locality < XMLRule
      def xpath
        "/html/body/div[@id='Main']/div/div[@id='col-container']/div[@id='side-col']/div[@id='a-bit-more-about']/dl/dd/span/span[@class='locality']"
        #"//span[@class='locality']"
      end
      
      protected
      
      def handle_val(val, dest_receiver)
        dest_receiver.locality = val
      end
    end
    
    class Country < XMLRule
      def xpath
        "/html/body/div[@id='Main']/div/div[@id='col-container']/div[@id='side-col']/div[@id='a-bit-more-about']/dl/dd/span/span[@class='country-name']"
        #"//span[@class='country-name']"
      end
      
      protected
      
      def handle_val(val, dest_receiver)
        dest_receiver.country = val
      end
    end
    
    class FullHometown < XMLRule
      def xpath
        "/html/body/div[@id='Main']/div/div[@id='col-container']/div[@id='side-col']/div[@id='a-bit-more-about']/dl[./dt/text() = 'Hometown:']/dd"
        #"//dl[./dt/text() = 'Hometown:']/dd"
      end
      
      protected
      
      def handle_val(val, dest_receiver)
        dest_receiver.full_hometown = val
      end
    end
    
    class Occupation < XMLRule
      def xpath
        "/html/body/div[@id='Main']/div/div[@id='col-container']/div[@id='side-col']/div[@id='a-bit-more-about']/dl[./dt/text() = 'Occupation:']/dd"
        #"//dl[./dt/text() = 'Occupation:']/dd"
      end
      
      protected
      
      def handle_val(val, dest_receiver)
        dest_receiver.occupation = val
      end
    end
    
    class Gender < XMLRule
      def xpath
        "/html/body/div[@id='Main']/div/div[@id='col-container']/div[@id='side-col']/div[@id='a-bit-more-about']/dl[./dt/text() = 'I am:']/dd[contains(text(),'Male') or contains(text(),'Female')]"
        #"//dl[./dt/text() = 'I am:']/dd[contains(text(),'Male') or contains(text(),'Female')]"
      end
      
      protected
      
      def preproces_val(val)
        val.replace "female" if val.include? "Female"
        val.replace "male" if val.include? "Male"
      end
      
      def handle_val(val, dest_receiver)
        dest_receiver.gender = val
      end
    end
    
    class RelStatus < XMLRule
      def xpath
        "/html/body/div[@id='Main']/div/div[@id='col-container']/div[@id='side-col']/div[@id='a-bit-more-about']/dl[./dt/text() = 'I am:']/dd[contains(text(),'Single') or contains(text(),'Taken')) ]"
        #"//dl[./dt/text() = 'I am:']/dd[contains(text(),'Single') or contains(text(),'Taken')) ]"
      end
      
      protected
      
      def preproces_val(val)
        val.replace "single" if val.include? "Single"
        val.replace "engaged" if val.include? "Taken"
      end
      
      def handle_val(val, dest_receiver)
        dest_receiver.rel_status = val
      end
    end
    
    class Email < XMLRule
      def xpath
        "/html/body/div[@id='Main']/div/div[@id='col-container']/div[@id='side-col']/div[@id='a-bit-more-about']/dl[./dt/text() = 'Email:']/dd"
        #"//dl[./dt/text() = 'Email:']/dd"
      end
      
      protected
      
      def preproces_val(val)
        val.gsub! /\s*/, ''
        val.gsub! /\[at\]/, '@'
      end
      
      def handle_val(val, dest_receiver)
        dest_receiver.email = val
      end
    end
    
    
    protected
    
    def initialize
      super()
      @collections[:NickName] = NickName.instance
      @collections[:FirstName] = FirstName.instance
      @collections[:LastName] = LastName.instance
      @collections[:Locality] = Locality.instance
      @collections[:Country] = Country.instance
      @collections[:FullHometown] = FullHometown.instance
      @collections[:Occupation] = Occupation.instance
      @collections[:Gender] = Gender.instance
      @collections[:RelStatus] = RelStatus.instance
      @collections[:Email] = Email.instance
    end
    
  end
  
end